/* eslint-disable no-unused-expressions */
import { message } from 'antd';
import router from 'umi/router';
import {
  getPageList,
  getFmPay,
  fetchSubmitList,
  queryDictItemsByCodes,
  cancleFmPay,
  getSecond,
  insertFmPay,
  getRrtData,
} from '@/services/payapply';
import { getReceive } from '@/services/paybill';

export default {
  state: {
    activeKey: '1',
    formFieldsValue: {},
    pageData: {
      list: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 10,
      },
    },
    detailData: {},
    expandForm: false,
    // 支付状态
    payStatusList: [],
    // 支付用途
    payPurposeList: [],
    userType: '',
    steps: [{ title: '选择业务数据' }, { title: '完善支付信息' }, { title: '完成' }],
    payApplySubmitData: {
      firstStep: {},
      secondStep: {},
    },
    // 单选切换按钮
    switchColumnsList: {},
    // 搜索选择下拉数据源
    searchListDataSource: {},
    // 第一步表格数据
    submitPageData: {
      list: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 10,
      },
    },
    staticPaginationData: {
      list: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 10,
      },
    },
    // 业务数据
    businessData: [],
    // 第一步搜索项数据
    stepFormFieldsValue: {},
    payAmount: '',
    receiveList: [],
  },

  effects: {
    // 查询接单方
    *fetchReceive({ payload }, { call, put }) {
      const res = yield call(getReceive, payload);
      if (res.success) {
        yield put({
          type: 'getReceive',
          payload: res.data,
        });
      } else {
        message.warning(res.message);
      }
    },
    *getSecond({ payload }, { call, put }) {
      const { feeType, bizType } = payload;
      const res = yield call(getSecond, payload);
      if (res.success) {
        yield put({
          type: 'handleBusiData',
          payload: {
            data: res.data,
            feeType,
            bizType,
          },
        });
      } else {
        message.warning(res.message || '服务端错误');
      }
    },
    // 取消支付申请
    *cancleFmPay({ payload }, { call }) {
      const res = yield call(cancleFmPay, payload);
      if (res.success) {
        message.success('操作成功');
        router.goBack();
      } else {
        message.warning(res.message || '服务端错误');
      }
    },
    // 字典
    *queryDictItemsByCodes({ payload }, { call, put }) {
      const res = yield call(queryDictItemsByCodes, payload);
      if (res.success) {
        yield put({
          type: 'updateSearchDataSource',
          payload: res.data,
        });
      } else {
        message.warning(res.message || '服务端错误');
      }
    },
    // 获取支付详情
    *getFmPay({ payload }, { call, put }) {
      const res = yield call(getFmPay, payload);
      if (res.success) {
        yield put({
          type: 'updateModelData',
          payload: {
            detailData: res.data,
          },
        });
      } else {
        message.warning(res.message || '服务端错误');
      }
      return res.data;
    },
    // 判断系统
    *judgmentSystem(_, { put, select }) {
      const systemList = yield select(state => state.login.systemList);
      let userType;
      systemList.forEach(item => {
        if (item.isSelected) {
          userType = item.sysCode;
        }
      });
      yield put({
        type: 'updateModelData',
        payload: { userType },
      });
    },
    // 查找列表
    *fetchList({ payload }, { call, put }) {
      const {
        pageArg: { currentPage, pageSize },
      } = payload;
      const response = yield call(getPageList, payload);
      if (response.success) {
        yield put({
          type: 'getList',
          payload: {
            data: response.data,
            currentPage,
            pageSize,
          },
        });
      } else {
        message.warning(response.message || '服务端错误');
      }
    },
    // 支付提交
    *insertFmPay({ payload }, { call }) {
      const res = yield call(insertFmPay, payload);
      if (res.success) {
        return res.data;
      }
      message.warning(res.message || '服务端错误');
      return false;
    },
    // 查找支付数据
    *fetchRrtData({ payload }, { call }) {
      const res = yield call(getRrtData, payload);
      if (res.success) {
        return res.data;
      }
      message.warning(res.message || '服务端错误');
      return false;
    },
    // 查找列表
    *fetchSubmitList({ payload }, { call, put }) {
      const {
        pageArg: { currentPage, pageSize },
      } = payload;
      let params;
      // 1，2，客服系统。1返款支付 2客户费用退费
      // 3，4 全国       3返款支付 4付费账单支付
      // 5，6 运行系统    5社保账单支付 6公积金账单支付
      if (payload.payapplytype === '1' || payload.payapplytype === '3') {
        payload.switchFlag
          ? (params = {
              accuRefundInfoVo: payload.params,
              pageArg: payload.pageArg,
            })
          : (params = {
              insRefundInfoVo: payload.params,
              pageArg: payload.pageArg,
            });
        params.accuRefundInfoVo
          ? (params.accuRefundInfoVo.state = 2)
          : (params.insRefundInfoVo.state = 2);
      } else {
        params = {
          ...payload.params,
          pageArg: payload.pageArg,
        };
      }
      if (payload.payapplytype === '3') {
        params.accuRefundInfoVo
          ? (params.accuRefundInfoVo.dataType = 1)
          : (params.insRefundInfoVo.dataType = 1);
      }
      const response = yield call(fetchSubmitList, {
        params,
        payapplytype: payload.payapplytype,
        switchFlag: payload.switchFlag,
      });
      if (response.success) {
        yield put({
          type: 'getSubmitList',
          payload: {
            data: response.data,
            currentPage,
            pageSize,
          },
        });
      } else {
        message.warning(response.message || '服务端错误');
      }
    },
  },

  reducers: {
    // 列表查询
    getList(
      state,
      {
        payload: { data, currentPage, pageSize },
      }
    ) {
      const list = Array.isArray(data.pageRecords) ? data.pageRecords : [];
      return {
        ...state,
        pageData: { list, pagination: { total: data.totalCount, current: currentPage, pageSize } },
      };
    },
    getReceive(state, { payload }) {
      return {
        ...state,
        receiveList: Array.isArray(payload) ? payload : [],
      };
    },
    // 获取列表
    getSubmitList(
      state,
      {
        payload: { data, currentPage, pageSize },
      }
    ) {
      const list = Array.isArray(data.pageRecords) ? data.pageRecords : [];
      return {
        ...state,
        submitPageData: {
          list,
          pagination: { total: data.totalCount, current: currentPage, pageSize },
        },
      };
    },
    // 初始化页码数据
    initialPageData(state, { payload }) {
      // 1，2，客服系统。1返款支付 2客户费用退费
      // 3，4 全国       3返款支付 4付费账单支付
      // 5，6 运行系统    5社保账单支付 6公积金账单支付
      // supplier 签约方
      // searchListRadioValue如果没有单选的话应该传给后台什么值
      const searchListOs5 = [
        { label: '账户名称', prop: 'insAccoName', type: 'input' },
        { label: '账单年月', prop: 'dateArr', type: 'dateYm' },
        { label: '缴费方式', prop: 'payType', type: 'select', dataSource: 'PAY_FEES_WAY' },
      ];
      const searchListOs6 = JSON.parse(JSON.stringify(searchListOs5));
      searchListOs6.splice(0, 1, { label: '账户名称', prop: 'accuAccoName', type: 'input' });
      const columsCs1 = [
        { label: '客户名称', prop: 'custName' },
        { label: '姓名', prop: 'empName' },
        { label: '证件号', prop: 'certNo', cellTextAlign: 'center', headerTextAlign: 'center' },
        // { label: '地区', prop: 'areaName' },
        { label: '服务类型', prop: 'serTypeIdName' },
        {
          label: '返款时间',
          prop: 'creTime',
          type: 'date',
          cellTextAlign: 'center',
          headerTextAlign: 'center',
        },
        {
          label: '本次返款金额(元)',
          prop: 'refundAmt',
          cellTextAlign: 'right',
          headerTextAlign: 'right',
        },
      ];
      const columsCs2 = [
        { label: '编号', prop: 'billNo' },
        { label: '客户名称', prop: 'custName' },
        { label: '成本中心', prop: 'costCenterName' },
        { label: '账单年月', prop: 'billYm', cellTextAlign: 'center', headerTextAlign: 'center' },
        {
          label: '账单金额(元)',
          prop: 'billAmo',
          cellTextAlign: 'right',
          headerTextAlign: 'right',
        },
      ];
      const columnsNs3 = JSON.parse(JSON.stringify(columsCs1));
      columnsNs3.splice(0, 1, { label: '签约方名称', prop: 'ordSndrName' });
      const columnsNs4 = [
        { label: '编号', prop: 'billNo' },
        { label: '接单方名称', prop: 'recvSuppName' },
        { label: '账单年月', prop: 'billYmStr', type: 'dateYm' },
        {
          label: '账单金额(元)',
          prop: 'billAmo',
          cellTextAlign: 'right',
          headerTextAlign: 'right',
        },
      ];
      const columnsOs5 = [
        { label: '账单年月', prop: 'billYm' },
        { label: '账户名称', prop: 'insAccoName' },
        { label: '缴费方式', prop: 'payTypeName' },
        { label: '涉及人数', prop: 'perNum' },
        { label: '险种', prop: 'assArrName' },
        { label: '账单金额(元)', prop: 'payAmo', cellTextAlign: 'right', headerTextAlign: 'right' },
      ];
      const columnsOs6 = JSON.parse(JSON.stringify(columnsOs5));
      columnsOs6.splice(1, 1, { label: '账户名称', prop: 'accuAccoName' });
      const firstStep = [];
      firstStep[1] = {
        tip:
          '说明：收款人为客户时可提交该客户下多个员工的返款支付，收款人为员工时只可提交一个员工的返款支付',
        searchList: [
          {
            label: '返款类型',
            prop: 'radio',
            type: 'radio',
            dataSource: [
              { itemCode: '1', itemName: '社保返款' },
              { itemCode: '2', itemName: '公积金返款' },
            ],
            initialValue: '1',
          },
          { label: '客户名称', prop: 'custId', type: 'customer' },
          { label: '姓名', prop: 'empName', type: 'input' },
        ],
        columns: columsCs1,
        rowKey: 'refundInfoId',
      };
      firstStep[2] = {
        searchList: [
          { label: '客户名称', prop: 'custId', type: 'customer' },
          { label: '账单年月', prop: 'dateArr', type: 'dateYm' },
        ],
        columns: columsCs2,
        rowKey: 'billId',
        searchListRadioValue: '3',
      };
      firstStep[3] = {
        tip:
          '说明：收款人为签约方时可提交该签约方下多个员工的返款支付，收款人为员工时只可提交一个员工的返款支付',
        searchList: [
          {
            label: '返款类型',
            prop: 'radio',
            type: 'radio',
            dataSource: [
              { itemCode: '1', itemName: '社保返款支付' },
              { itemCode: '2', itemName: '公积金返款支付' },
            ],
            initialValue: '1',
          },
          { label: '签约方名称', prop: 'suppId', type: 'supplier' },
          { label: '姓名', prop: 'empName', type: 'input' },
        ],
        columns: columnsNs3,
        rowKey: 'refundInfoId',
      };
      firstStep[4] = {
        searchList: [
          {
            label: '费用类型',
            prop: 'radio',
            type: 'radio',
            dataSource: [
              { itemCode: '3', itemName: '账单费用' },
              { itemCode: '4', itemName: '一次性费用' },
            ],
            initialValue: '3',
          },
          { label: '接单方名称', prop: 'recvSuppId', type: 'recvSupp' },
          { label: '账单年月', prop: 'dateArr', type: 'dateYm' },
        ],
        columns: columnsNs4,
        rowKey: 'billId',
      };
      firstStep[5] = {
        searchList: searchListOs5,
        columns: columnsOs5,
        rowKey: 'billId',
        searchListRadioValue: '5',
      };
      firstStep[6] = {
        searchList: searchListOs6,
        columns: columnsOs6,
        rowKey: 'billId',
        searchListRadioValue: '6',
      };
      const switchColumnsList = {
        searchList: {
          searchList1: JSON.parse(JSON.stringify(firstStep[1].searchList)),
          searchList3: JSON.parse(JSON.stringify(firstStep[3].searchList)),
          searchList4: [
            {
              label: '费用类型',
              prop: 'radio',
              type: 'radio',
              dataSource: [
                { itemCode: '3', itemName: '账单费用' },
                { itemCode: '4', itemName: '一次性费用' },
              ],
              initialValue: '3',
            },
            { label: '接单方名称', prop: 'recvSuppId', type: 'recvSupp' },
            { label: '业务年月', prop: 'dateArr', type: 'dateYm' },
          ],
        },
        columns: {
          columns1: JSON.parse(JSON.stringify(columsCs1)),
          columns3: JSON.parse(JSON.stringify(columnsNs3)),
          columns4: [
            { label: '编号', prop: 'onceFeePayNo' },
            { label: '接单方名称', prop: 'ordRecvName' },
            { label: '业务年月', prop: 'busiYmLong', type: 'dateYm' },
            {
              label: '收费总金额(元)',
              prop: 'feeAmo',
              cellTextAlign: 'right',
              headerTextAlign: 'right',
            },
          ],
        },
        rowKey: {
          rowKey1: 'refundInfoId',
          rowKey3: 'refundInfoId',
          rowKey4: 'onceFeePayId',
        },
      };
      const secondStep = [
        {},
        {
          label: '收款人类型',
          type: 'radio',
          dataSource: [{ itemCode: '1', itemName: '客户' }, { itemCode: '2', itemName: '员工' }],
        },
        { label: '收款人类型', type: 'text', text: '客户', value: '1' },
        {
          label: '收款人类型',
          type: 'radio',
          dataSource: [{ itemCode: '5', itemName: '签约方' }, { itemCode: '2', itemName: '员工' }],
        },
        { label: '收款人类型', type: 'text', text: '接单方', value: '6' },
        { label: '收款人类型', type: 'text', text: '社保局', value: '4' },
        { label: '收款人类型', type: 'text', text: '公积金中心', value: '3' },
      ];

      return {
        ...state,
        payApplySubmitData: {
          firstStep: firstStep[payload],
          secondStep: secondStep[payload],
        },
        switchColumnsList,
      };
    },
    // 字典
    updateSearchDataSource(state, { payload }) {
      return {
        ...state,
        searchListDataSource: {
          PAY_FEES_WAY: payload.PAY_FEES_WAY || [],
          BANK_ID: payload.BANK_ID || [],
        },
        payStatusList: payload.FM_PAY_STATE || [],
        payPurposeList:
          payload.OS_FM_PAY_TYPE || payload.CS_FM_PAY_TYPE || payload.JH_FM_PAY_TYPE || [],
      };
    },
    handleBusiData(state, { payload }) {
      // 1，2，客服系统。1返款支付(1.1社保返款   1.2公积金返款) 2客户费用退费 (2.3)
      // 3，4 全国       3返款支付 (3.1 社保返款支付  3.2 公积金返款支付)      4付费账单支付(4.3 账单费用 4.4 一次性费用)
      // 5，6 运行系统    5社保账单支付(5.5) 6公积金账单支付(6.6)
      const { feeType, bizType, data } = payload;
      let dataSource = [];
      switch (+bizType) {
        case 1:
          if (+feeType === 1) {
            dataSource = data.insRefundInfoList;
          } else {
            dataSource = data.accuRefundInfoList;
          }
          break;
        case 2:
          dataSource = data.responseBillList;
          break;
        case 3:
          if (+feeType === 1) {
            dataSource = data.insRefundInfoList;
          } else {
            dataSource = data.accuRefundInfoList;
          }
          break;
        case 4:
          if (+feeType === 3) {
            dataSource = data.responseBillList;
          } else {
            dataSource = data.responseOnceFeePayList;
          }
          break;
        case 5:
          dataSource = data.responseInsBill;
          break;
        case 6:
          dataSource = data.responseAccuBill;
          break;
        default:
          break;
      }
      return {
        ...state,
        businessData: dataSource,
        payAmount: data.sumAmt,
      };
    },
    handlePaginationData(state, { payload }) {
      const { data, current, pageSize } = payload;
      const deepData = JSON.parse(JSON.stringify(data));
      const list = deepData.splice((current - 1) * pageSize, pageSize);
      return {
        ...state,
        staticPaginationData: {
          list,
          pagination: {
            current,
            pageSize,
            total: data.length,
          },
        },
      };
    },
    updateModelData(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
